#!/bin/bash
######################################################################
# Script for building the gloox Lua bindings for Tessa
#
# Due to the way swig (the binding code generator) works, 
# it is necessary for us to include some of our own files 
# to the bindings.
#
# Requirements:
#
# gloox source, which you can get from http://camaya.net/glooxdownload
# gloox bindings, available at: http://camaya.net/glooxbindings
# liblua5.1(-dev), the Lua library
# 
# gloox must already be built and installed (make, sudo make install)
# before you run this script.
#
# It is ESSENTIAL that the version of gloox that the bindings 
# are based on is the very same version that you make available 
# to this script.
#######################################################################
# Configuration     #
#####################
# Path to the extracted gloox archive:
GLOOX_SRC_DIR=../../gloox-0.9-rc4/

# Path to extracted gloox bindings archive:
GLOOX_BINDINGS_DIR=../../glooxbindings-0.1.3/

# Other requirements # # # # # # # # # # # # # # # # # # # # # # # #

LUA_INCLUDE_DIR=/usr/include/lua5.1/
SYS_LIB_PATH=/usr/local/lib

####################################################################
# Initialisation
GLOOX_SRC_SUBDIR=${GLOOX_SRC_DIR%"/"}"/src"
GLOOX_BINDINGS_SUBDIR=${GLOOX_BINDINGS_DIR%"/"}"/src"
#####################################

echo -e '\E[1;31m'"\033[1;47mConfiguring gloox...\033[0m"
pushd $GLOOX_SRC_DIR
if ! test -e config.h
then ./configure
fi

cp config.h src/
popd

rm gloox/gloox > /dev/null
ln -s ../$GLOOX_SRC_SUBDIR gloox/gloox

echo -e '\E[1;31m'"\033[1;47mGenerating binding code...\033[0m"
swig -lua -Wall -ignoremissing -c++ -I$GLOOX_BINDINGS_SUBDIR -I$GLOOX_SRC_SUBDIR -Igloox -o gloox_wrap.cxx gloox/tessa.i

echo -e '\E[1;31m'"\033[1;47mCompiling code...\033[0m"
g++ -fpic -c $GLOOX_SRC_SUBDIR/*.cpp gloox_wrap.cxx -I${LUA_INCLUDE_DIR} -Igloox -I$GLOOX_SRC_SUBDIR

echo -e '\E[1;31m'"\033[1;47mBuilding Lua module...\033[0m"
g++ -shared *.o  -L$SYS_LIB_PATH -lresolv -lresolv -lgnutls -lz -o gloox.so

echo -e '\E[1;31m'"\033[1;47mDone\033[0m"

echo -e '\E[1;31m'"\033[1;47mYou must now install gloox.so to the Tessa folder, or your Lua path\033[0m"
which lua5.1 >/dev/null && echo "Your Lua path is:" $(lua -e "print(package.cpath)") || echo "(usually /usr/local/lib/lua/5.1/)"
